<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="com.example.softwareconfigurationcoursedesign.UserService" %>
<%@ page import="com.example.softwareconfigurationcoursedesign.User" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>会员管理</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }

        table, th, td {
            border: 1px solid black;
        }

        th, td {
            padding: 8px;
            text-align: left;
        }

        button {
            padding: 5px 10px;
            background-color: #0074cc;
            color: white;
            border: none;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056a7;
        }
    </style>
</head>
<body>
<%
    Connection connection;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String jdbcUrl = "jdbc:mysql://localhost:3306/course_design";
        String username = "root";
        String password = "123456";
        connection = DriverManager.getConnection(jdbcUrl, username, password);
        connection.createStatement();
    } catch (Exception ignored) {
    } finally {
    }
%>
<h1>会员列表</h1>
<table id="UserTable">
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>手机号</th>
        <th>密码</th>
        <th>操作</th> <!-- 添加操作列 -->
    </tr>
    <!-- 使用JSP动态生成会员列表 -->
    <%
        UserService userService = new UserService();
        List<User> userList = userService.getUserListFromDatabase(); // 从数据库中获取会员列表
        for (User user : userList) {
    %>
    <tr>
        <td><%= user.getUserId() %>
        </td>
        <td><%= user.getName() %>
        </td>
        <td><%= user.getPhoneNumber() %>
        </td>
        <td><%= user.getPassword() %>
        </td>
        <td>
            <button onclick="openEditUserWindow()">编辑</button>
            <br><!-- 传递会员ID到编辑页面 -->
            <form action="deleteUser" method="POST"> <!-- 添加删除表单 -->
                <input type="hidden" name="userId" value="<%= user.getUserId() %>"> <!-- 隐藏域，存储会员ID -->
                <button type="submit" onclick="window.location.href='Admin.html'">删除</button>
            </form>
        </td>
    </tr>
    <%
        }
    %>
    <script>
        function openAddUserWindow() {
            // Open a new window for adding a user
            window.open("AddUser.jsp", "_blank", "width=400, height=300");
        }

        function openEditUserWindow() {
            window.open("editUser.jsp", "_blank", "width=400,height=300");
        }
    </script>
</table>
<button id="addUserBtn" onclick="openAddUserWindow()">添加会员</button>
</body>
</html>